//
//  UIViewController+YFHelper.m
//  YunFu
//
//  Created by Bager on 2018/9/12.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "UIViewController+YFHelper.h"

@implementation UIViewController (YFHelper)

- (UIViewController*)sh_topMostController
{
    UIViewController *topController = self ;
    
    while ([self presentedViewController])
        topController = [topController presentedViewController];
    
    return topController;
}

- (UIViewController*)sh_currentViewController
{
    UIViewController *currentViewController = [self sh_topMostController];
    
    while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
        currentViewController = [(UINavigationController*)currentViewController topViewController];
    
    return currentViewController;
}

//我们在非视图类中想要随时展示一个view时，需要将被展示的view加到当前view的子视图，或用当前view presentViewController，或pushViewContrller，这些操作都需要获取当前正在显示的ViewController。
//获取当前view的UIViewController
+ (UIViewController *)sh_currentViewControllerFromcurrentView
{
    UIViewController *result = nil;
    
    // 1. get current window
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows) {
            if (tempWindow.windowLevel == UIWindowLevelNormal) {
                window = tempWindow;
                break;
            }
        }
    }
    
    // 2. get current View Controller
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    return result;
}

//获取当前屏幕中present出来的viewcontroller。
- (UIViewController *)getPresentedViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    
    return topVC;
}

@end
